home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / math.swg / 0100_Finding a Percent..pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-22  |  1.5 KB  |  44 lines

  1. {I am having trouble finding exact percents in BPC Pascal 7.0. While this
  2. routine will compile under TPC 4.0+, I am still having troubles getting it
  3. to come out to a rounded number. I have posted this message in other
  4. conferences than Pascal simply because it's a math problem as well as a
  5. programming problem. Thanks Moderators! }
  6.  
  7.   function calc_p2(num1,num2:integer):string15;
  8.   var
  9.     x:real;
  10.     z:integer;
  11.     cp:string[5];
  12.   begin
  13.     if num1=0 then calc_p2:='0' else calc_p2:='100';
  14.     cp:='  0';
  15.     if (num1=0) or (num2=0) then
  16.       begin
  17.         calc_p2:='    0';
  18.         exit;
  19.       end;
  20.     x:=num1/num2*100;
  21.     str(x:1:1,cp);
  22.     if cp='100.00' then cp:='100';
  23.     if cp='100.0' then cp:='100';
  24.     if cp='0.0' then cp:='0';
  25.     if cp='0.00' then cp:='0';
  26.     while length(cp)<5 do insert(' ',cp,1);
  27.     calc_p2:=cp;
  28.   end;
  29.  
  30. begin
  31.   writeln('50 into 100 = ',calc_p2(50,100));
  32.   writeln('25 into 100 = ',calc_p2(50,100));
  33. end.
  34.  
  35. The problem that I am having is, is that it isn't always accurate. I have
  36. a program that has 13 different categories, each with an amount in each
  37. category. Those are then calculated with the percents to show total number
  38. against the full number of all categories. Unfortunatly, adding up the
  39. percents comes up with 99.90% or 100.1% and even 100.3%.
  40.  
  41. IS there a way to get this to be ACCURATE? It's driving me nutts. Maybe I
  42. am just approaching it in the wrong direction, but any help would be
  43. appreciated.
  44.